interface Color {
  R: number;
  G: number;
  B: number;
}
interface GradualColorRampItem {
  start: Color;
  end: Color;
}

/**
 * 对给定起始颜色值的渐变色带进行插值
 * @param gradualColorRamp 包含起始颜色值的色带对象
 * @param level 插值数
 * @returns 颜色值数组
 */
const getColorArray = (
  gradualColorRamp: GradualColorRampItem,
  level: number,
): string[] => {
  // const gradualRamp = colorRampList.find(ramp => ramp.name === colorRamp)?.ramp as { start: Color; end: Color }
  if (level <= 2) {
    return [
      `rgb(${gradualColorRamp.start.R}, ${gradualColorRamp.start.G}, ${gradualColorRamp.start.B})`,
      `rgb(${gradualColorRamp.end.R}, ${gradualColorRamp.end.G}, ${gradualColorRamp.end.B})`,
    ];
  } else {
    const startR = gradualColorRamp.start.R;
    const startG = gradualColorRamp.start.G;
    const startB = gradualColorRamp.start.B;

    const endR = gradualColorRamp.end.R;
    const endG = gradualColorRamp.end.G;
    const endB = gradualColorRamp.end.B;

    const stepR = (endR - startR) / (level - 1);
    const stepG = (endG - startG) / (level - 1);
    const stepB = (endB - startB) / (level - 1);

    const colorRampArray = [];
    for (let i = 0; i < level - 1; i++) {
      colorRampArray.push(
        `rgb(${(startR + stepR * i).toFixed(6)}, ${(startG + stepG * i).toFixed(6)}, ${(startB + stepB * i).toFixed(6)})`,
      );
    }
    colorRampArray.push(`rgb(${endR}, ${endG}, ${endB})`);
    return colorRampArray;
  }
};

const getRandomColor = () => {
  // 生成随机的 R、G、B 分量值
  const r = Math.floor(Math.random() * 256); // 0 ~ 255
  const g = Math.floor(Math.random() * 256); // 0 ~ 255
  const b = Math.floor(Math.random() * 256); // 0 ~ 255

  // 将 R、G、B 分量值转换为十六进制字符串
  const hexR = r.toString(16).padStart(2, '0'); // 转换为两位的十六进制，并在不足两位时前面加上 0
  const hexG = g.toString(16).padStart(2, '0');
  const hexB = b.toString(16).padStart(2, '0');

  // 拼接十六进制颜色码并返回
  const hexColor = `#${hexR}${hexG}${hexB}`;
  return hexColor;
};

export { getColorArray, getRandomColor };
export type { Color, GradualColorRampItem };
